#include<math.h> // For "float fabs (float val)"

struct coordinate{
	float x;
	float y;
};

unsigned char getDirection(struct coordinate MotorPos, struct coordinate NextPos){
	float tmp;

	if(NextPos.y < MotorPos.y)tmp = MotorPos.y - 1;
	if(NextPos.y > MotorPos.y)tmp = MotorPos.y + 1;
	if(NextPos.y == MotorPos.y)tmp = MotorPos.y;

	if(fabs(NextPos.y-tmp)<=0.5){//45 degree continue
		if(NextPos.y < MotorPos.y){
			if(NextPos.x > MotorPos.x)return 4;//Right Down
			if(NextPos.x < MotorPos.x)return 6;//Left Down
		}
		if(NextPos.y > MotorPos.y){
			if(NextPos.x > MotorPos.x)return 6;//Right Up
			if(NextPos.x < MotorPos.x)return 8;//Left Up
		}
	}
	else{
		if( (MotorPos.y > NextPos.y) && (NextPos.y-tmp)>0.5){
			if(MotorPos.x < NextPos.x)return 5;//Right
			if(MotorPos.x > NextPos.x)return 1;//Left
		}
		if( (MotorPos.y > NextPos.y) && (NextPos.y-tmp)<-0.5)return 3;//Down

		if( (MotorPos.y < NextPos.y) && (NextPos.y-tmp)<-0.5){
			if(MotorPos.x < NextPos.x)return 5;//Right
			if(MotorPos.x > NextPos.x)return 1;//Left
		}
		if( (MotorPos.y < NextPos.y) && (NextPos.y-tmp)>0.5)return 7;//Up
	}
	if(MotorPos.x == NextPos.x){
		if(MotorPos.y < NextPos.y)return 7;//Up
		if(MotorPos.y > NextPos.y)return 3;//Down
		if(MotorPos.y = NextPos.y)return 0;//Stop
	}
	return 0;
}